//
//  smHourseSearchController.m
//  shimaoHouse
//
//  Created by amy zhang on 12-6-27.
//  Copyright (c) 2012年 yst. All rights reserved.
//

#import "smHourseSearchController.h"
#import "smHourseDetailController.h"
#import "HTTPImageView.h"
#import "SUPApplication.h"
#import "smMboSmMboDB.h"
#import "SUPConnectionProperties.h"
#import "SUPLoginCredentials.h"
#import "SUPConnectionProfile.h"
#import "SUPObjectList.h"
#import "smMboSmTbHouse.h"

@interface smHourseSearchController ()<UITableViewDelegate, UITableViewDataSource,UISearchBarDelegate>

@property (nonatomic,strong) UITableView *hourseTableView;


@end

@implementation smHourseSearchController

@synthesize hourseTableView=_hourseTableView;
@synthesize houseList=_houseList;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    //[self setTitle:@"房源查询"];
    [self.navigationController.navigationBar setHidden:YES];//隐藏title
    
    
    UIImage* titleImg = [UIImage imageNamed:@"house_title.png"];    
    UIImageView *titleView =[[UIImageView alloc] initWithImage:titleImg];
    [titleView setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 70)];
    
    [self.view addSubview:titleView];
    
    
    self.hourseTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 165, self.view.bounds.size.width, 
                                                                         self.view.bounds.size.height-150) 
                                                        style:UITableViewStylePlain];  
    
    [self.hourseTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.hourseTableView setDelegate:self];
    [self.hourseTableView setDataSource:self];
    
    UISearchBar *searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 70, self.view.bounds.size.width, 95)];  
    searchBar.placeholder=@"输入房源名称查询";  
    searchBar.delegate =self;  
    //self.hourseTableView.tableHeaderView = searchBar;  
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;  
    searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;  
    UIImage* searchbarImg = [UIImage imageNamed:@"search.png"];
    [searchBar setBackgroundImage:searchbarImg];
    [self.view addSubview:searchBar];
    
    
    [self.view addSubview: self.hourseTableView];
    
   


}

- (void)viewWillAppear:(BOOL)animated {
    
    NSMutableArray *array = [[NSMutableArray alloc] init];
    
    [smMboSmMboDB beginSynchronize];
    SUPObjectList* list=[smMboSmTbHouse findAll];
    
    NSString* temp = [NSString stringWithFormat:@"houselist size is %d",[list size]];
    
    NSLog(temp,nil);
    
    if ([list length] > 0)
    {
        for (smMboSmTbHouse * oneRec in list)
        {
            [array addObject:oneRec];
        }
    }
    self.houseList = array;
    
    [self.hourseTableView reloadData];
    [super viewWillAppear:animated]; 
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}// Default is 1 if not implemented


//tableview行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    
    return [self.houseList count];
    //return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    } 
    
    for (UIView *v in [cell subviews]) {
        if ([v isKindOfClass:[UILabel class]] |
            [v isKindOfClass:[HTTPImageView class]]|
            [v isKindOfClass:[UISearchBar class]]) {
            [v removeFromSuperview];
        }
    }
    
    //右边加箭头
    //cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
    
    
    // Set up the cell...
	NSUInteger row = [indexPath row];
    smMboSmTbHouse *house = [self.houseList objectAtIndex:row];
    
    //加载图片
    UIImage* tempImg = [UIImage imageNamed:@"photoloading.png"];
    
    //UIImageView *imgView =[[UIImageView alloc] initWithImage:tempImg];
    
     HTTPImageView* imgView = [[HTTPImageView alloc] init];     
     [imgView setUrl:[NSString stringWithFormat:@"http://10.91.2.14:8080/upload_file/mobile/%@",[house imagePath]]];
    [imgView setPlaceHolder:tempImg];
     [imgView setFrame:CGRectMake(15, 5, 200, 155)];
    
    [cell addSubview:imgView];
    
    
    UILabel* hoursenameLabel = [[UILabel alloc] initWithFrame:CGRectMake(230, 5, 400, 30)];
    [hoursenameLabel setBackgroundColor:[UIColor clearColor]];
    [hoursenameLabel setText:[NSString stringWithFormat:@"位置：%@",[house name]]];
    //[hoursenameLabel setText:@"新明半岛"];
    [hoursenameLabel setTextColor:[UIColor colorWithRed:9/255.0 green:119/255.0 blue:179/255.0 alpha:1.0]];
    hoursenameLabel.font = [UIFont systemFontOfSize:29];
    [cell addSubview:hoursenameLabel];

    UILabel* statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(230, 40, 400, 30)];
    [statusLabel setBackgroundColor:[UIColor clearColor]];
    [statusLabel setText:[NSString stringWithFormat:@"当前状态：%@",[house status]]];
    //[statusLabel setText:@"asasda"];
    statusLabel.font=[UIFont systemFontOfSize:22];
    [statusLabel setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];
    [cell addSubview:statusLabel];
       
    UILabel* sizelable = [[UILabel alloc]initWithFrame:CGRectMake(230, 70, 150, 30)];
     [sizelable setBackgroundColor:[UIColor clearColor]];
    sizelable.text = [NSString stringWithFormat:@"面积：%@",[house area]];
    //sizelable.text=@"面积120";    
    sizelable.font=[UIFont systemFontOfSize:22];
    [sizelable setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];;
    [cell addSubview:sizelable];
    
    UILabel* moneylable = [[UILabel alloc]initWithFrame:CGRectMake(230, 100, 150, 30)];
     [moneylable setBackgroundColor:[UIColor clearColor]];
    NSUInteger a=[house unitPrice].intValue*[house area].intValue;
    moneylable.text = [NSString stringWithFormat:@"总价：%d万",a/10000];
   // moneylable.text=@"1212";
    moneylable.font=[UIFont systemFontOfSize:22];
    [moneylable setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];
    [cell addSubview:moneylable];
    
    UILabel* huxinglable = [[UILabel alloc]initWithFrame:CGRectMake(230, 130, 300, 30)];
     [huxinglable setBackgroundColor:[UIColor clearColor]];
    huxinglable.text = [NSString stringWithFormat:@"户型：%@",[house huxing]];
    //huxinglable.text=@"liangshiyiding";
    huxinglable.font=[UIFont systemFontOfSize:22];
    [huxinglable setTextColor:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];
    [cell addSubview:huxinglable];

    
    UILabel* priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(600, 20, 300, 30)];
    [priceLabel setBackgroundColor:[UIColor clearColor]];
    [priceLabel setText:[NSString stringWithFormat:@"%@元/平方米",[house unitPrice]]];
    //spriceLabel.text=@"1211";    
    priceLabel.font=[UIFont systemFontOfSize:22];
    [cell addSubview:priceLabel];
    priceLabel.textColor=[UIColor redColor];
    
    UILabel* status1Label = [[UILabel alloc] initWithFrame:CGRectMake(650, 80, 300, 30)];
    [status1Label setBackgroundColor:[UIColor clearColor]];
    //[status1Label setText:@"在售"];
    [status1Label setText:[NSString stringWithFormat:@"%@",[house status]]];
    status1Label.font=[UIFont systemFontOfSize:22];
    [cell addSubview:status1Label];
    status1Label.textColor=[UIColor blueColor];
    
    
    UIImage* seletedImg = [UIImage imageNamed:@"tableview_seletedBakground.png"];    
    UIImageView *seletedImgView =[[UIImageView alloc] initWithImage:seletedImg];
    [cell setSelectedBackgroundView:seletedImgView];
        
    
    UIImage* backImg = [UIImage imageNamed:@"tableview_background.png"];    
    UIImageView *backImgView =[[UIImageView alloc] initWithImage:backImg];
    [cell setBackgroundView:backImgView];
    
    return cell;
    
    
}


//row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        
    return 171;
}



//点击项目执行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    smHourseDetailController *detailController=[[smHourseDetailController alloc] init];
    
    NSUInteger row = [indexPath row];
    smMboSmTbHouse *house = [self.houseList objectAtIndex:row];
    [detailController setitems:house ];
    
    [self.navigationController pushViewController:detailController animated:YES];
    
    
}

//搜索框查询事件
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    [searchBar resignFirstResponder];//取消键盘
    SUPQuery *query=[SUPQuery getInstance];
    query.testCriteria=[SUPAttributeTest match:@"name" :searchBar.text];
    SUPObjectList *l=[smMboSmTbHouse findWithQuery:query];
    NSString* temp = [NSString stringWithFormat:@"house searchlist size is %d",[l size]];
    NSLog(temp,nil);
    NSMutableArray *array = [[NSMutableArray alloc] init];
    if ([l length] > 0)
    {
        for (smMboSmTbHouse * oneRec in l)
        {
            [array addObject:oneRec];
           // NSString* t=[NSString stringWithFormat:@"mingcheng:%@",[oneRec name]];
            //NSLog(t,nil);
        }
    }
    self.houseList = array;
    
    [self.hourseTableView reloadData];

}

@end
